import { constants } from '@/views/project_request/constants';

function initHistoryLabourInput(monthCount) {
    let arrLabour;
    let arrCost;
    (arrLabour = []).length = monthCount;
    (arrCost = []).length = monthCount;
    arrLabour.fill(0);
    arrCost.fill(0);
    return [
        { input: arrLabour, rowName: '实际投入(人月)' },
        { input_type: 4, input: arrCost, rowName: '实际成本(RMB)' }
    ];
}

function getMonthsInPeriods(startDate, endDate) {
    const start = new Date(startDate);
    const end = new Date(endDate);
    let monthCount;
    let currYear = start.getFullYear();
    let currMonth = start.getMonth();
    monthCount = (end.getFullYear() - start.getFullYear()) * 12;
    monthCount -= start.getMonth();
    monthCount += end.getMonth();
    let months = [];
    for (let i = 0; i < monthCount + 1; i++) {
        if (currMonth < 12) {
            currMonth += 1;
        } else {
            currYear += 1;
            currMonth = 1;
        }
        months.push(`${currYear}年${currMonth}月`);
    }
    return months;
}

function getProjectPeriods(start, end, historical, current) {
    let nextMonths;
    let pastMonths;
    let startDate = new Date(start);
    let endDate = new Date(end);
    let currentDate = new Date(current);
    let lastMonthDate = new Date().setMonth(currentDate.getMonth() - 1 , 1);
    if (historical === true) {
        pastMonths = getMonthsInPeriods(startDate, lastMonthDate);
        nextMonths = getMonthsInPeriods(currentDate, endDate);
    } else {
        pastMonths = [];
        nextMonths = getMonthsInPeriods(startDate, endDate);
    }
    console.log(`pastMonths: ${pastMonths}, nextMonths: ${nextMonths}`);
    return {
        pastMonths: pastMonths,
        nextMonths: nextMonths
    };
}

function sumHistoryTotalManMonth(inputs) {
    let filterInputs = inputs
        .filter(item => item.isTotal === false)
        .map(function(v) {
            return v.historyInput;
        });
    let sum = filterInputs
        .reduce(function(a, b) {
            return a.concat(b);
        }, [])
        .reduce(function(a, b) {
            return parseFloat(a || '0') + parseFloat(b || '0');
        }, 0);
    return sum;
}

function sumTotalManMonthList(inputList) {
    let sumManMonthList = [];
    for (let rowIndex = 0; rowIndex < inputList.length; rowIndex++) {
        if (!inputList[rowIndex].isTotal) {
            for (
                let inputIndex = 0;
                inputIndex < inputList[rowIndex].input.length;
                inputIndex++
            ) {
                if (sumManMonthList[inputIndex] === undefined) {
                    sumManMonthList[inputIndex] = 0;
                }
                sumManMonthList[inputIndex] += parseFloat(
                    inputList[rowIndex].input[inputIndex] || '0'
                );
            }
        }
    }
    console.log('sumManMonthList', sumManMonthList);
    return sumManMonthList;
}

function getBusinessType(id) {
    const businessTypeOptions = constants.businessOptions;
    for (const businessTypeOption of businessTypeOptions) {
        if (businessTypeOption.children.find(x => x.value === id.toString())) {
            return businessTypeOption.value;
        }
    }
}


function getProductionCategory(id) {
    const productionOptions = constants.productionOptions;
    for (const productionOption of productionOptions) {
        if (productionOption.children.find(x => x.value === id.toString())) {
            return productionOption.value;
        }
    }
}

export {
    initHistoryLabourInput,
    getMonthsInPeriods,
    getProjectPeriods,
    sumHistoryTotalManMonth,
    sumTotalManMonthList,
    getBusinessType,
    getProductionCategory
};
